package com.diamondfsd.jooq.methods;

import com.diamondfsd.jooq.learn.codegen.tables.records.S1UserRecord;
import org.jooq.DSLContext;

import static com.diamondfsd.jooq.learn.codegen.Tables.S1_USER;

public class InsertMethod {

  public static void insertByDSL(DSLContext dslContext) {
    Integer userId = dslContext.insertInto(S1_USER, S1_USER.USERNAME, S1_USER.ADDRESS, S1_USER.EMAIL)
        .values("username1", "insertByDSL", "diamondfsd@gmail.com")
        //唯一键重复时，更新或忽略onDuplicateKeyIgnore，
        // 当更新时，sql：insert into sq_user values(xxx,xxx) on duplicate key update s1_user set username=xxx,address=xxx
//        .onDuplicateKeyUpdate()
//        .set(S1_USER.USERNAME,"duplicateKey-update")
//        .set(S1_USER.ADDRESS,"update")
        .returning(S1_USER.ID)
//        .execute();
        .fetchOne().getId();
  }

  public static void insertByRecord(DSLContext dslContext) {
    S1UserRecord record = dslContext.newRecord(S1_USER);
    record.setUsername("usernameRecord1");
    record.setEmail("diamondfsd@gmail.com");
    record.setAddress("address hello");
    record.insert();
// 这里的id是插入后数据库返回的自增ID，会自动存入record中，可以通过get方法获取
    record.getId();
  }
}
